package org.advanced.security;

/**
 * @author John.zhang
 * @version Id: ThreadLocal, v 1.0 2020/12/9 15:30 ZhiYong Exp $
 */
public class ThreadLocals {

    private static Integer NUM = 0;

    public static final ThreadLocal<Integer> LOCAL = ThreadLocal.withInitial(() -> 0);

    public static void main(String[] args) {
        Thread[] threads = new Thread[5];
        int max = 5;
        for (int i = 0; i < max; i++) {
            threads[i] = new Thread(() -> {
                NUM += 5;
                Integer num = LOCAL.get();
                num += 5;
                LOCAL.set(num);
                System.out.println(Thread.currentThread().getName() + "->" + NUM);
                System.out.println(Thread.currentThread().getName() + "->" + num);
            }, "Thread" + i);
        }

        for (Thread thread : threads) {
            thread.start();
        }

    }

}
